package w83b.w83bOperacionesPaso.w83bActas;

import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;


import w83b.w83bBeans.W83bListaEscrutiniosBean;
import es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP;
import es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE;

// TODO: Auto-generated Javadoc
/**
 * The Class W83bTablaAgrupacionesOP.java.
 *
 * @author Deusto Sistemas
 */
public class W83bBuscarNumeroVotosOutOPAction extends Q70ActionPasoOP{

	
	protected void processOP(Q70ContextoEJIE contexto, ActionErrors errores, ActionMapping mapping,
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//Guardamos la accion en el contexto
	}

	protected ActionForward processForward(Q70ContextoEJIE contexto, ActionErrors errors, ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject jsonObject = new JSONObject();
		HashMap lista=new HashMap();
		int suma=0;
		if(request.getParameter("pestana").equals("EscrutiniosC1")){
			if(contexto.get("w83bListaEscrutiniosC1")!=null){
				lista=(HashMap)contexto.get("w83bListaEscrutiniosC1");
				Collection c=lista.values();
				Iterator it=c.iterator();
				while(it.hasNext()){
					W83bListaEscrutiniosBean escrutinio=(W83bListaEscrutiniosBean)it.next();
					suma=suma+Integer.parseInt(escrutinio.getNumVotos());
				}
			}
			jsonObject.put("sumaVotosC1", suma);
		}
		else{
			if(contexto.get("w83bListaEscrutiniosC2")!=null){
				lista=(HashMap)contexto.get("w83bListaEscrutiniosC2");
				Collection c=lista.values();
				Iterator it=c.iterator();
				while(it.hasNext()){
					W83bListaEscrutiniosBean escrutinio=(W83bListaEscrutiniosBean)it.next();
					suma=suma+Integer.parseInt(escrutinio.getNumVotos());
				}
			}
			jsonObject.put("sumaVotosC2", suma);
		}
		response.setContentType("text/javascript;charset=iso-8859-1");
		response.setHeader("Pragma", "cache");
		response.setHeader("Expires", "0");
		response.setHeader("Cache-Control", "private");
		response.setStatus(HttpServletResponse.SC_OK);
		PrintWriter out = response.getWriter();
		out.print(jsonObject.toString());
		out.flush();
		
		return null;
	}
	


}
